
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.contrib.humanize &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="消息框架" href="messages.html" />
    <link rel="prev" title="部署 GeoDjango" href="gis/deployment.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="gis/deployment.html" title="部署 GeoDjango">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="messages.html" title="消息框架">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-humanize">
            
  <div class="section" id="s-module-django.contrib.humanize">
<span id="s-django-contrib-humanize"></span><span id="module-django.contrib.humanize"></span><span id="django-contrib-humanize"></span><h1><code class="docutils literal notranslate"><span class="pre">django.contrib.humanize</span></code><a class="headerlink" href="#module-django.contrib.humanize" title="永久链接至标题">¶</a></h1>
<p>一套 Django 模板过滤器，用于为数据添加 “人情味”。</p>
<p>要激活这些过滤器，请在你的 <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 配置中添加 <code class="docutils literal notranslate"><span class="pre">'django.contrib.humanize'</span></code>。添加完毕后，在模板中使用 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">humanize</span> <span class="pre">%}</span></code>，你就可以使用以下过滤器了。</p>
<div class="section" id="s-apnumber">
<span id="s-std:templatefilter-apnumber"></span><span id="apnumber"></span><span id="std:templatefilter-apnumber"></span><h2><code class="docutils literal notranslate"><span class="pre">apnumber</span></code><a class="headerlink" href="#apnumber" title="永久链接至标题">¶</a></h2>
<p>对于数字 1-9，返回拼出的数字。否则，返回数字。这遵循了美联社的风格。</p>
<p>举例：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">1</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">one</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">2</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">two</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">10</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">10</span></code>。</li>
</ul>
<p>你可以传入一个整数或整数的字符串表示。</p>
</div>
<div class="section" id="s-intcomma">
<span id="s-std:templatefilter-intcomma"></span><span id="intcomma"></span><span id="std:templatefilter-intcomma"></span><h2><code class="docutils literal notranslate"><span class="pre">intcomma</span></code><a class="headerlink" href="#intcomma" title="永久链接至标题">¶</a></h2>
<p>将一个整数或浮点数（或其中之一的字符串表示法）转换为每三位数包含逗号的字符串。</p>
<p>举例：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">4500</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">4,500</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">4500.2</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">4,500.2</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">45000</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">45,000</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">450000</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">450,000</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">4500000</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">4,500,000</span></code>。</li>
</ul>
<p><a class="reference internal" href="../../topics/i18n/formatting.html"><span class="doc">本地格式化</span></a> 如果启用，例如使用 <code class="docutils literal notranslate"><span class="pre">''de</span></code> 语言，将得到尊重：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">45000</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">'45.000'</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">450000</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">'450.000'</span></code>。</li>
</ul>
</div>
<div class="section" id="s-intword">
<span id="s-std:templatefilter-intword"></span><span id="intword"></span><span id="std:templatefilter-intword"></span><h2><code class="docutils literal notranslate"><span class="pre">intword</span></code><a class="headerlink" href="#intword" title="永久链接至标题">¶</a></h2>
<p>将大整数（或整数的字符串表示）转换为友好的文本表示。将 <code class="docutils literal notranslate"><span class="pre">1.0</span></code> 翻译成单数，将所有其他数值翻译成复数，这对某些语言可能不正确。对于 100 万以上的数字效果最好。</p>
<p>举例：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">1000000</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">1.0</span> <span class="pre">million</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">1200000</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">1.2</span> <span class="pre">million</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">1200000000</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">1.2</span> <span class="pre">billion</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">-1200000000</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">-1.2</span> <span class="pre">billion</span></code>。</li>
</ul>
<p>支持最高 10^100（Googol）的数值。</p>
<p><a class="reference internal" href="../../topics/i18n/formatting.html"><span class="doc">本地格式化</span></a> 如果启用，例如使用 <code class="docutils literal notranslate"><span class="pre">''de</span></code> 语言，将得到尊重：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">1000000</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">'1,0</span> <span class="pre">Million'</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">1200000</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">'1,2</span> <span class="pre">Millionen'</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">1200000000</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">'1,2</span> <span class="pre">Milliarden'</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">-1200000000</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">'-1,2</span> <span class="pre">Milliarden'</span></code>。</li>
</ul>
<div class="versionchanged">
<span class="title">Changed in Django 3.1:</span> <p>增加了对负整数的支持。</p>
</div>
</div>
<div class="section" id="s-naturalday">
<span id="s-std:templatefilter-naturalday"></span><span id="naturalday"></span><span id="std:templatefilter-naturalday"></span><h2><code class="docutils literal notranslate"><span class="pre">naturalday</span></code><a class="headerlink" href="#naturalday" title="永久链接至标题">¶</a></h2>
<p>对于当天或一天之内的日期，根据情况返回 “today”、“tomorrow” 或 “yesterday”。否则，使用传入的格式字符串格式化日期。</p>
<p><a href="#id1"><span class="problematic" id="id2">**</span></a>参数：<a href="#id3"><span class="problematic" id="id4">**</span></a>日期格式化字符串，如 <a class="reference internal" href="../templates/builtins.html#std:templatefilter-date"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">date</span></code></a> 标签所述。</p>
<p>例子（当 'today' 是 2007 年 2 月 17 日）：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">16</span> <span class="pre">Feb</span> <span class="pre">2007</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">yesterday</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">today</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">18</span> <span class="pre">Feb</span> <span class="pre">2007</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">tomorrow</span></code>。</li>
<li>任何其他的日子都会根据给定的参数进行格式化，如果没有给定参数，则根据 <a class="reference internal" href="../settings.html#std:setting-DATE_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DATE_FORMAT</span></code></a> 配置。</li>
</ul>
</div>
<div class="section" id="s-naturaltime">
<span id="s-std:templatefilter-naturaltime"></span><span id="naturaltime"></span><span id="std:templatefilter-naturaltime"></span><h2><code class="docutils literal notranslate"><span class="pre">naturaltime</span></code><a class="headerlink" href="#naturaltime" title="永久链接至标题">¶</a></h2>
<p>对于日期时间值，返回一个字符串，代表多少秒、多少分钟或多少小时前 —— 如果值超过一天，则返回到 <a class="reference internal" href="../templates/builtins.html#std:templatefilter-timesince"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">timesince</span></code></a> 格式。如果日期时间值是在未来，返回值将自动使用一个适当的短语。</p>
<p>例子（当 'now' 是 2007 年 2 月 17 日 16:30:00）：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">16:30:00</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">now</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">16:29:31</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">29</span> <span class="pre">seconds</span> <span class="pre">ago</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">16:29:00</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">minute</span> <span class="pre">ago</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">16:25:35</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">4</span> <span class="pre">minutes</span> <span class="pre">ago</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">15:30:29</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">59</span> <span class="pre">minutes</span> <span class="pre">ago</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">15:30:01</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">59</span> <span class="pre">minutes</span> <span class="pre">ago</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">15:30:00</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">an</span> <span class="pre">hour</span> <span class="pre">ago</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">13:31:29</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">2</span> <span class="pre">hours</span> <span class="pre">ago</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">16</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">13:31:29</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">day,</span> <span class="pre">2</span> <span class="pre">hours</span> <span class="pre">ago</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">16</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">13:30:01</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">day,</span> <span class="pre">2</span> <span class="pre">hours</span> <span class="pre">ago</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">16</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">13:30:00</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">day,</span> <span class="pre">3</span> <span class="pre">hours</span> <span class="pre">ago</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">16:30:30</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">30</span> <span class="pre">seconds</span> <span class="pre">from</span> <span class="pre">now</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">16:30:29</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">29</span> <span class="pre">seconds</span> <span class="pre">from</span> <span class="pre">now</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">16:31:00</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">minute</span> <span class="pre">from</span> <span class="pre">now</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">16:34:35</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">4</span> <span class="pre">minutes</span> <span class="pre">from</span> <span class="pre">now</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">17:30:29</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">an</span> <span class="pre">hour</span> <span class="pre">from</span> <span class="pre">now</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">17</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">18:31:29</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">2</span> <span class="pre">hours</span> <span class="pre">from</span> <span class="pre">now</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">18</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">16:31:29</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">day</span> <span class="pre">from</span> <span class="pre">now</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">26</span> <span class="pre">Feb</span> <span class="pre">2007</span> <span class="pre">18:31:29</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">week,</span> <span class="pre">2</span> <span class="pre">days</span> <span class="pre">from</span> <span class="pre">now</span></code>。</li>
</ul>
</div>
<div class="section" id="s-ordinal">
<span id="s-std:templatefilter-ordinal"></span><span id="ordinal"></span><span id="std:templatefilter-ordinal"></span><h2><code class="docutils literal notranslate"><span class="pre">ordinal</span></code><a class="headerlink" href="#ordinal" title="永久链接至标题">¶</a></h2>
<p>将一个整数转换为字符串的序数。</p>
<p>举例：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">1</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">1st</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">2</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">2nd</span></code>。</li>
<li><code class="docutils literal notranslate"><span class="pre">3</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">3rd</span></code>。</li>
</ul>
<p>你可以传入一个整数或整数的字符串表示。</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="docutils literal notranslate"><span class="pre">django.contrib.humanize</span></code></a><ul>
<li><a class="reference internal" href="#apnumber"><code class="docutils literal notranslate"><span class="pre">apnumber</span></code></a></li>
<li><a class="reference internal" href="#intcomma"><code class="docutils literal notranslate"><span class="pre">intcomma</span></code></a></li>
<li><a class="reference internal" href="#intword"><code class="docutils literal notranslate"><span class="pre">intword</span></code></a></li>
<li><a class="reference internal" href="#naturalday"><code class="docutils literal notranslate"><span class="pre">naturalday</span></code></a></li>
<li><a class="reference internal" href="#naturaltime"><code class="docutils literal notranslate"><span class="pre">naturaltime</span></code></a></li>
<li><a class="reference internal" href="#ordinal"><code class="docutils literal notranslate"><span class="pre">ordinal</span></code></a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="gis/deployment.html"
                        title="上一章">部署 GeoDjango</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="messages.html"
                        title="下一章">消息框架</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/contrib/humanize.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="gis/deployment.html" title="部署 GeoDjango">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="messages.html" title="消息框架">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>